import sys
import os
import logging
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# 找到的视频页面URLs
video_page_urls = [
    "https://music.2t58.com/video/list.html",
    "https://music.2t58.com/mv/list.html",
    "https://music.2t58.com/list/video.html",
    "https://music.2t58.com/list/mv.html",
]

def get_full_page_content(url):
    """获取完整的页面内容，禁用SSL验证"""
    try:
        logger.info(f"尝试获取完整页面: {url}")
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(url, headers=headers, timeout=60, verify=False)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        logger.info(f"成功获取完整页面: {url}")
        return response.text
    except Exception as e:
        logger.error(f"获取页面失败: {url}, 错误: {e}")
        return None

def analyze_video_page(url):
    """分析视频页面，寻找用户提供的HTML结构"""
    content = get_full_page_content(url)
    
    if not content:
        return False
    
    try:
        soup = BeautifulSoup(content, 'html.parser')
        
        # 1. 查找用户提供的.video_list容器
        video_list_container = soup.select_one('.video_list')
        
        if video_list_container:
            logger.info(f"在 {url} 中找到 .video_list 容器")
            
            # 保存包含.video_list容器的部分HTML
            with open("video_list_container.html", "w", encoding="utf-8") as f:
                f.write(str(video_list_container))
            
            logger.info("已保存 .video_list 容器内容到 video_list_container.html")
            
            # 解析.video_list容器内的内容
            
            # 查找标题
            title_element = video_list_container.select_one('.title h1')
            if title_element:
                logger.info(f"页面标题: {title_element.get_text().strip()}")
            
            # 查找视频总数
            video_count_span = video_list_container.select_one('.pagedata span')
            if video_count_span:
                logger.info(f"视频总数: {video_count_span.get_text().strip()}")
            
            # 查找视频项列表
            video_items = video_list_container.select('ul li')
            
            if video_items:
                logger.info(f"找到 {len(video_items)} 个视频项")
                
                # 解析前5个视频项的信息
                videos = []
                for i, item in enumerate(video_items[:5]):
                    try:
                        # 提取视频图片
                        img_element = item.select_one('.pic img')
                        image_url = img_element.get('src') if img_element else ''
                        
                        # 提取视频链接和标题
                        name_a_element = item.select_one('.name a')
                        if name_a_element:
                            video_url = urljoin(url, name_a_element.get('href', ''))
                            full_title = name_a_element.get_text().strip()
                            title_attr = name_a_element.get('title', '')
                            
                            logger.info(f"视频 {i+1}:")
                            logger.info(f"  文本: {full_title}")
                            logger.info(f"  标题属性: {title_attr}")
                            logger.info(f"  URL: {video_url}")
                            logger.info(f"  图片URL: {image_url}")
                    except Exception as e:
                        logger.warning(f"解析视频项 {i+1} 失败: {e}")
                        continue
                
                return True
            else:
                logger.warning("在 .video_list 容器中未找到视频项列表")
        else:
            logger.warning(f"在 {url} 中未找到 .video_list 容器")
            
            # 显示页面中的所有div容器类名，帮助调试
            all_div_classes = set()
            for div in soup.find_all('div'):
                if div.get('class'):
                    all_div_classes.add(' '.join(div.get('class')))
            logger.info(f"页面中找到的所有div容器类名: {all_div_classes}")
            
            # 尝试查找其他可能的视频列表容器
            logger.info("尝试查找其他可能的视频列表容器...")
            
            # 查找包含ul和li结构的容器
            ul_elements = soup.find_all('ul')
            for i, ul in enumerate(ul_elements[:10]):  # 只检查前10个
                li_count = len(ul.find_all('li'))
                if li_count > 5:  # 假设视频列表至少有5个视频
                    logger.info(f"找到可能的视频列表容器 (ul {i+1}): 包含 {li_count} 个li元素")
                    
                    # 检查是否有图片和链接的结构
                    has_images = False
                    for li in ul.find_all('li')[:3]:
                        img = li.find('img')
                        a = li.find('a')
                        if img and a:
                            has_images = True
                            break
                    
                    if has_images:
                        logger.info("  该容器包含图片和链接，可能是视频列表")
                        # 保存这个容器的内容
                        with open(f"possible_video_list_{i+1}.html", "w", encoding="utf-8") as f:
                            f.write(str(ul))
                        logger.info(f"  已保存容器内容到 possible_video_list_{i+1}.html")
        
    except Exception as e:
        logger.error(f"分析页面失败: {e}")
        import traceback
        traceback.print_exc()
    
    return False

def main():
    logger.info("开始分析视频页面结构...")
    
    for url in video_page_urls:
        logger.info(f"\n分析页面: {url}")
        
        if analyze_video_page(url):
            logger.info(f"\n成功在 {url} 中找到并解析了视频列表结构")
            # 找到一个有效的视频页面后就可以停止了
            break
    
    logger.info("\n视频页面分析完成")
    
    # 如果没有找到.video_list容器，使用用户提供的HTML结构作为参考创建模拟数据
    if not os.path.exists("video_list_container.html"):
        logger.info("\n未找到实际的.video_list容器，将使用用户提供的HTML结构创建模拟数据文件")
        
        # 用户提供的视频列表HTML结构
        mock_video_list_html = """
<div class="video_list">
    <div class="title">
        <h1>MV视频列表</h1>
        <div class="pagedata">共有：<span>1677</span>个视频</div>
    </div>
    <ul>
        <li>
            <div class="pic"><a href="/video/d3NjbW14bXdj.html" target="_mp3"><img src="https://s.5bb3.com/img4.kuwo.cn/wmvpic/324/44/87/1970217680.jpg?param=300y150" height="150" width="300"><div class="play"><span class="fa fa-play"></span></div></a></div>
            <div class="name"><a href="/video/d3NjbW14bXdj.html" title="心若琉璃" target="_mp3">心若琉璃 - 刘惜君</a></div>
        </li>
        <li>
            <div class="pic"><a href="/video/d3Nzc3d2c212.html" target="_mp3"><img src="https://s.5bb3.com/img4.kuwo.cn/wmvpic/324/s4s69/95/587618454.jpg?param=300y150" height="150" width="300"><div class="play"><span class="fa fa-play"></span></div></a></div>
            <div class="name"><a href="/video/d3Nzc3d2c212.html" title="敢" target="_mp3">敢 - 黄龄</a></div>
        </li>
    </ul>
</div>
"""
        
        with open("mock_video_list.html", "w", encoding="utf-8") as f:
            f.write(mock_video_list_html)
        
        logger.info("已创建模拟视频列表HTML文件: mock_video_list.html")

if __name__ == "__main__":
    main()